package com.mmy.controller.bidtender;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.mmy.domain.bidtender.TBidDetailed;
import com.mmy.domain.bidtender.TBidTrip;
import com.mmy.domain.bidtender.VBidInvite;
import com.mmy.domain.bidtender.VBidTender;
import com.mmy.domain.constparameter.TConstParameter;
import com.mmy.domain.sys.TSysOrg;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.impl.bidtender.BidDetailedServiceImpl;
import com.mmy.service.impl.bidtender.BidInviteServiceImpl;
import com.mmy.service.impl.bidtender.BidTenderServiceImpl;
import com.mmy.service.impl.bidtender.BidTripServiceImpl;
import com.mmy.service.impl.constparameter.ConstParameterServiceImpl;
import com.mmy.service.impl.sys.SysOrgServiceImpl;

@Controller
public class BidTenderController {
	@Autowired
	BidTenderServiceImpl bidTenderServiceImpl;
	@Autowired
	BidInviteServiceImpl bidInviteServiceImpl;
	@Autowired
	BidTripServiceImpl bidTripServiceImpl;
	@Autowired
	BidDetailedServiceImpl bidDetailedServiceImpl;
	@Autowired
	SysOrgServiceImpl sysOrgServiceImpl;
	@Autowired
	ConstParameterServiceImpl constParameterServiceImpl;

	/**
	 * 函数功能说明： 个人中心显示本人发起的投标信息
	 *
	 * @param 参数说明 vBidTender
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.10.29
	 */
	@RequestMapping(value = "/mytender")
	public String myTender(String pageNumber, String pageSize, Model model, HttpSession session) {
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		// 获得所有招标信息
		List<VBidTender> lstBidTender = bidTenderServiceImpl.findByCreator(pageNumber, pageSize, sessionSysUser);
		model.addAttribute("lstBidTender", lstBidTender);
		HashMap<String, Integer> mapPage = bidTenderServiceImpl.mapTender(pageNumber, pageSize, sessionSysUser);
		model.addAttribute("iPageNo", mapPage.get("iPageNo"));
		model.addAttribute("iTotalCount", mapPage.get("iTotalCount"));
		model.addAttribute("iMaxPage", mapPage.get("iMaxPage"));
		model.addAttribute("iPageSize", mapPage.get("iPageSize"));
		return "permanagement/MyTender";
	}

	/**
	 * 函数功能说明： 前台个人中心查看投标信息详情
	 *
	 * @param 参数说明 tBidTender
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.10.29
	 */
	@RequestMapping(value = "/mytenderinfo")
	public String toMyTenderInfo(HttpServletRequest request, Model mBidTender) {
		// 查看投标基本信息
		VBidTender dbBidTender = bidTenderServiceImpl.findByTenderID(request.getParameter("TenderID"));
		mBidTender.addAttribute("dbBidTender", dbBidTender);
		// 查看投标行程信息
		List<TBidTrip> lstBidTrip = bidTripServiceImpl.findByTenderID(request.getParameter("TenderID"));
		mBidTender.addAttribute("lstBidTrip", lstBidTrip);
		// 查看投标明细信息
		List<TBidDetailed> lstBidDetailed = bidDetailedServiceImpl.findByTenderID(request.getParameter("TenderID"));
		mBidTender.addAttribute("lstBidDetailed", lstBidDetailed);

		return "permanagement/MyTenderInfo";
	}

	/**
	 * 函数功能说明： 后台查看选择的招标所对应的投标信息表
	 *
	 * @param 参数说明 tBidTender
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.10.29
	 */
	@RequestMapping(value = "/backtobidtenderindex")
	public String backToBidTenderIndex(HttpServletRequest request, Model model) {

		String strInviteID = request.getParameter("strInviteID");
		List<VBidTender> lstBidTender = bidTenderServiceImpl.findByInviteID(strInviteID);
		model.addAttribute("lstBidTender", lstBidTender);
		// 获取所有组织机构信息
		List<TSysOrg> lstSysOrg = sysOrgServiceImpl.findAll();
		model.addAttribute("lstSysOrg", lstSysOrg);

		model.addAttribute("strInviteID", strInviteID);
		return "webbackground/bidtendermanagement/BidTenderIndex";
	}

	/**
	 * 函数功能说明： 异步--近郊游列表数据
	 * 
	 * @param 参数说明 vBidInvite
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.02.22
	 *
	 */
	@RequestMapping("/backbidtenderjson")
	@ResponseBody
	public String backBidTenderJson(HttpServletRequest request, Model model) {

		// 接收的分页信息处理
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		int startIndex = (page - 1) * rows;
		int count = rows;
		// 搜索条件
		String strOrgID = request.getParameter("orgID");
		String strInviteID = request.getParameter("strInviteID");

		// 表格数据
		List<VBidTender> lstBidTender = bidTenderServiceImpl.findByOrgID(strOrgID, strInviteID, startIndex, count);

		model.addAttribute("lstBidTender", lstBidTender);
		// 数据总数量
		int total = bidTenderServiceImpl.queryTenderCount(strOrgID, strInviteID);
		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("rows", lstBidTender);
		ret.put("total", total);
		return JSONObject.toJSONString(ret);
	}

	/**
	 * 函数功能说明： 后台查看投标信息详情
	 *
	 * @param 参数说明 tBidTender
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.11.19
	 */

	@RequestMapping(value = "/backbidtenderinfo")
	public String toBidTenderInfo(HttpServletRequest request, Model model) {
		// 查看投标基本信息
		String strTender = request.getParameter("iTenderID");
		VBidTender dbBidTender = bidTenderServiceImpl.findByTenderID(strTender);
		model.addAttribute("dbBidTender", dbBidTender);
		// 查看投标行程信息
		List<TBidTrip> lstBidTrip = bidTripServiceImpl.findByTenderID(strTender);
		model.addAttribute("lstBidTrip", lstBidTrip);
		return "webbackground/bidtendermanagement/BidTenderInfo";
	}

	/**
	 * 函数功能说明： 后台 中标
	 *
	 * @param 参数说明 tBidTender
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.11.19
	 */
	@RequestMapping("/winbidtender")
	@ResponseBody
	public Byte winBidTender(HttpServletRequest request, HttpSession session) {
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		Byte iState = bidTenderServiceImpl.updateState(request.getParameter("iTenderID"),
				request.getParameter("iInviteID"), sessionSysUser);
		if (iState == 2) {
			bidInviteServiceImpl.updateInviteState(request.getParameter("iTenderID"), sessionSysUser);
		}
		return iState;
	}

	/**
	 * 函数功能说明： 前台查看选择的招标所对应的该登陆人公司的所有的投标信息表
	 *
	 * @param 参数说明 tBidTender
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.12.10
	 */
	@RequestMapping(value = "/tofrontbidtenderindex")
	public String toFrontBidTenderIndex(HttpServletRequest request, Model model, HttpSession session) {
		String strInviteName = request.getParameter("strInviteName");
		String strInviteID = request.getParameter("iInviteID");
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		List<VBidTender> lstBidTender = bidTenderServiceImpl.findByInviteIDAndOrgID(strInviteID,
				sessionSysUser.getOrgID());
		model.addAttribute("lstBidTender", lstBidTender);
		model.addAttribute("strInviteID", strInviteID);
		model.addAttribute("strInviteName", strInviteName);
		return "bidtender/BidTenderIndex";
	}

	/**
	 * 函数功能说明：前台 进入创建投标信息页面
	 *
	 * @param 参数说明 tBidTender
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.12.10
	 */
	// 点击参与投标按钮进入创建页面
	@RequestMapping("/tocreatebidtender")
	public String createBidTenderCheckedFind(HttpServletRequest request, Model model) {
		String strInviteName = request.getParameter("strInviteName");
		String strInviteID = request.getParameter("iInviteID");
		model.addAttribute("strInviteID", strInviteID);
		model.addAttribute("strInviteName", strInviteName);
		// 参数设置查询招标预算最大值

		String maxBudget = "招投标预算";
		TConstParameter dbConstParameter = constParameterServiceImpl.getByText(maxBudget);
		model.addAttribute("dbConstParameter", dbConstParameter);
		return "bidtender/CreateBidTender";
	}

	/**
	 * 函数功能说明：前台 创建投标信息
	 *
	 * @param 参数说明 tBidTender
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.12.10
	 */
	@RequestMapping(value = "/savecreatebidtender")
	@ResponseBody
	public String saveCreateBidTender(@RequestParam(value = "tender") String strJsonTender,
			@RequestParam("trip") String strJsonTrip, @RequestParam("detailed") String strJsonDetailed,
			HttpSession session) throws ParseException {

		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		VBidInvite dbBidInvite = bidTenderServiceImpl.createBidTender(strJsonTender, strJsonTrip, strJsonDetailed,
				sessionSysUser);

		return "redirect:tofrontbidtenderindex?iInviteID=" + dbBidInvite.getInviteID() + "&strInviteName="
				+ dbBidInvite.getInviteName();
	}

	/**
	 * 函数功能说明：前台 进入修改投标信息页面
	 *
	 * @param 参数说明 tBidTender
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.12.14
	 */
	@RequestMapping("/toupdatebidtender")
	public String toUpdateBidTender(HttpServletRequest request, Model model) {
		VBidTender dbBidTender = bidTenderServiceImpl.findByTenderID(request.getParameter("iTenderID"));
		model.addAttribute("dbBidTender", dbBidTender);
		// 查询该投标的行程表
		List<TBidTrip> lstBidTrip = bidTripServiceImpl.findByTenderID(request.getParameter("iTenderID"));
		model.addAttribute("lstBidTrip", lstBidTrip);
		// 查看投标明细信息
		List<TBidDetailed> lstBidDetailed = bidDetailedServiceImpl.findByTenderID(request.getParameter("iTenderID"));
		model.addAttribute("lstBidDetailed", lstBidDetailed);
		// 参数设置查询招标预算最大值
		String maxBudget = "招投标预算";
		TConstParameter dbConstParameter = constParameterServiceImpl.getByText(maxBudget);

		model.addAttribute("dbConstParameter", dbConstParameter);
		return "bidtender/UpdateBidTender";
	}

	/**
	 * 函数功能说明：前台 修改投标基本信息
	 *
	 * @param 参数说明 tBidTender
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.12.14
	 */
	@RequestMapping(value = "/saveupdatebidtender")
	@ResponseBody
	public String saveUpdateBidTender(@RequestParam(value = "bidTender") String strUpdateJsonTender,
			@RequestParam("bidTrip") String strUpdateJsonTrip,
			@RequestParam("bidDetailed") String strUpdateJsonDetailed, HttpSession session) throws ParseException {
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		VBidInvite dbBidInvite = bidTenderServiceImpl.updateBidTender(strUpdateJsonTender, strUpdateJsonTrip,
				strUpdateJsonDetailed, sessionSysUser);

		return "redirect:tofrontbidtenderindex?iInviteID=" + dbBidInvite.getInviteID() + "&strInviteName="
				+ dbBidInvite.getInviteName();
	}

	/**
	 * 函数功能说明：前台 投标信息提交
	 *
	 * @param 参数说明 tBidTender
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.12.14
	 */
	@RequestMapping("/toupdatebidtenderstate")
	@ResponseBody
	public String toUpdateBidTenderState(HttpServletRequest request, HttpSession session) {
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		String iState = Byte
				.toString(bidTenderServiceImpl.updateSubmitState(request.getParameter("iTenderID"), sessionSysUser));

		return iState;
	}

	/**
	 * 函数功能说明： 后台查看投标信息详情
	 *
	 * @param 参数说明 tBidTender
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.11.19
	 */

	@RequestMapping(value = "/frontbidtenderinfo")
	public String toFrontBidTenderInfo(HttpServletRequest request, Model model) {
		// 查看投标基本信息
		String strTender = request.getParameter("iTenderID");
		VBidTender dbBidTender = bidTenderServiceImpl.findByTenderID(strTender);
		model.addAttribute("dbBidTender", dbBidTender);
		// 查看投标行程信息
		List<TBidTrip> lstBidTrip = bidTripServiceImpl.findByTenderID(strTender);
		model.addAttribute("lstBidTrip", lstBidTrip);
		// 查看投标明细
		List<TBidDetailed> lstBidDetailed = bidDetailedServiceImpl.findByTenderID(strTender);
		model.addAttribute("lstBidDetailed", lstBidDetailed);
		return "bidtender/BidTenderInfo";
	}

}
